package com.just.bz_back.service.impl;

import com.just.bz_back.dao.TypeMapper;
import com.just.bz_back.model.Type;
import com.just.bz_back.constant.ConstantData;
import com.just.bz_back.model.TypeExample;
import com.just.bz_back.model.dto.JsonDataUtil;
import com.just.bz_back.service.RedisService;

import com.just.bz_back.service.TypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * 对type数据表进行处理需更新TYPE_LIST
 * @author : baibaiwucahng
 * @date : 2019/8/31  14:59
 */
@Service
public class TypeServiceImpl implements TypeService {
    @Resource
    private RedisService redisService;

    @Resource
    private TypeMapper typeMapper;


    @Override
    public List<Type> getAll() {
       String value = (String) redisService.get(ConstantData.TYPE_LIST);
        List<Type> types=JsonDataUtil.string2Obj(value,List.class);
        return types;
    }

    @Override
    public Type getTypeByid(Type type) {
        TypeExample typeExample=new TypeExample();
        typeExample.createCriteria().andIdEqualTo(type.getId());
        List<Type> type1 = typeMapper.selectByExample(typeExample);
        if (type1.size()==0)
        {
            return null;
        }
        return type1.get(0);
    }

    @Override
    public int updateType(Type type) {
        TypeExample typeExample=new TypeExample();
        typeExample.createCriteria().andIdEqualTo(type.getId());
        typeMapper.updateByExampleSelective(type,typeExample);
        return 0;
    }

    @Override
    public int insertType(Type type) {
        typeMapper.insertSelective(type);
        return 0;
    }

}
